package com.study.talk.ui.app.group.create

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.study.talk.ui.app.base.BaseActivity
import com.study.talk.databinding.ActivityCreateGroupBinding
import com.study.talk.netty.message.response.CreateGroupResponse
import com.study.talk.utils.alert.AlertUtil
import com.study.talk.utils.extras.showToast
import com.study.talk.utils.extras.toGroupInfo
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@AndroidEntryPoint
class CreateGroupActivity : BaseActivity() {

    private val binding by lazy { ActivityCreateGroupBinding.inflate(
        layoutInflater
    )}

    private val vm : CreateGroupViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        setSupportActionBar(binding.createGroupToolbar)
        initView()
    }


    private fun initView() {
        binding.createGroupToolbar.setNavigationOnClickListener {
            AlertUtil.showCustomDialog(
                this@CreateGroupActivity, "退出提醒", "确定退出创建群聊吗？已操作的数据将会丢失哦!"
            ) {
                if (it) finish()
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onCreateGroupMessage(response: CreateGroupResponse) {
        showToast( "创建群聊成功！")
        updateGroupList()
        lifecycleScope.launch {
            delay(1000)
            toGroupInfo(response.groupId)
            finish()
        }
    }
}